package de.gameduell.loader.src {
	import nl.demonsters.debugger.MonsterDebugger;

	import de.gameduell.framework.application.Settings;
	import de.gameduell.framework.asset.AssetEvent;
	import de.gameduell.framework.asset.AssetGroup;
	import de.gameduell.framework.debug.Debug;
	import de.gameduell.framework.util.FlashVars;
	import de.gameduell.framework.util.Tools;
	import de.gameduell.loader.src.ApplicationLoader;

	import flash.display.MovieClip;

	/**
	 * @author Gevorg Sargsyan
	 */
	public class TestLoader extends ApplicationLoader {

		protected var _clip:MovieClip;
		public static const LIBRARY_PATH:String = "libraries";

		public function TestLoader() {
			super();
		}

		override protected function catchVars():void {
			var obj:Object = root.loaderInfo.parameters;
			if(obj[FlashVars.LANGUAGE] == null) {
				obj[FlashVars.LANGUAGE] = "de";
			}
			obj[FlashVars.XML_FOLDER] = "build";
			obj["location"] = 6;
			FlashVars.catchVars(obj);
		}

		override protected function configLoaded(event:AssetEvent):void {
			//			config = XML(event.asset.data);
			//			var loaderGraphics:XML = config.child(LOADER).children()[0];
			//			Debug.trace("TestLoader::configLoaded() loaderGraphics=" + loaderGraphics);
			//			var loaderAsset:IAsset = createAsset(SWF, loaderGraphics, baseURL + "../../loader/build/loadergfx/loadergfx_default.swf");
			//			loaderAsset.addEventListener(AssetEvent.COMPLETE, initLoader);
			//			loaderAsset.load();
			try {  
				config = XML(event.asset.data);
				settings = new Settings(config.child(SETTINGS));
				isStandalone = settings.standalone;
				
				var loaderGFXPath:String = "../" + LIBRARY_PATH + "/src/de/gameduell/loader/build/loadergfx/loadergfx_default.swf";
				var str:String = "<loader><loadergfx>" + loaderGFXPath + "</loadergfx></loader>";				var loaderList:XMLList = new XMLList(str);
				//				var loaderList:XMLList = config.child(LOADER);
				//				loaderList.appendChild("<loadergfx>" + baseURL + "../../loader/build/" + "loadergfx/loadergfx_default.swf</loadergfx>");
				var loaderAssets:AssetGroup = new AssetGroup(LOADER, "");
				if(settings.useGeneralLangFile) {
					loaderList.appendChild(getGeneralLang());
				}
				loaderList.appendChild("<lang>lang/" + settings.gameType + "_" + FlashVars.getKeyValue(FlashVars.LANGUAGE) + ".xml</lang>");
				fillAssetGroup(loaderList, loaderAssets);
				
				loaderAssets.addEventListener(AssetEvent.COMPLETE, initLoader);
				loaderAssets.addEventListener(AssetEvent.LOAD_ERROR, loadError);
				loaderAssets.load();
			}catch(e:Error) {
				MonsterDebugger.trace("Error loading config: " + e.getStackTrace(), e, 0xAA0000);
				Debug.trace("Error loading config: " + e.getStackTrace(), Debug.ERROR);
				Tools.traceToFrontend("Error loading config");
			}
		}	

		/**
		 * Extracts all needed parts of the loader url.
		 * <b>baseURL</b> loader url without any url parameters and file name
		 * <b>appURL</b> path to application related assets
		 */
		override protected function extractUrl():void {
			var _url:String = root.loaderInfo.url;
			_url = _url.substr(0, _url.indexOf("?"));
			_url = _url.substr(0, _url.lastIndexOf("/") + 1);
			isOnline = false;
			//_url.indexOf( "http://" ) == 0 || _url.indexOf( "https://" ) == 0;
			var xmlfolder:String = FlashVars.getKeyValue(FlashVars.XML_FOLDER) != FlashVars.NO_VALUE ? FlashVars.getKeyValue(FlashVars.XML_FOLDER) : "build";  
			appURL = "" + xmlfolder + "/";
			baseURL = "";
			
			if( isOnline == false ) {
				appURL = unescape(appURL);
			}
			FlashVars.setKeyValue(FlashVars.APP_URL, appURL);
		}

		override protected function getGeneralLang():String {
			return "<general_lang>../" + LIBRARY_PATH + "/src/de/gameduell/game/lang/general" + "_" + FlashVars.getKeyValue("language") + ".xml</general_lang>";
		}

		override protected function getUrls(node:XML):String {
			if(String(node.parent().name()) == COMMUNICATION  ) {
				return "../" + LIBRARY_PATH + "/src/de/gameduell/net/build/standalone.swf";
			} else {
				var localized:Boolean = getAttribute(node, LOCALIZED) != "";
				
				if(String(node.name()) == "loadergfx" ) {
					return String(node.text());
				}
				if(String(node.name()) == "general_lang" ) {
					return String(node.text()).split("*").join(FlashVars.getKeyValue("language"));
				}
				else if(localized) {
					return appURL + String(node.text()).split("*").join(FlashVars.getKeyValue("language"));
				} else {
					return appURL + node.text();
				}
			}
			return null;
		}		
	}
}
